﻿using System;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using Core.Authenticated;
using Core.Library;
using DataObject;
using DataObject.Article;
using DataObject.Category;
using DataObject.Comment;
using DataObject.Product;

namespace CMS.Controls.Comment
{
    public partial class CommentManager : AuthenticatedControl
    {
        protected int WaittingForPublish;
        protected int Publish;
        protected int Status;
        protected int TypeId;
        protected int PageIndex;
        protected int PageSize;

        protected void Page_Load(object sender, EventArgs e)
        {
            TypeId      = Utils.GetIdFromRequestString(Utils.TypeId, 0);                       
            Status      = Utils.GetIdFromRequestString(Utils.Status, 0);                       
            PageIndex   = Utils.GetIdFromRequestString(Utils.PageIndex, SettingValues.PageIndex);
            PageSize    = Utils.GetIdFromRequestString(Utils.PageSize, ConvertUtility.ToInt32(ddlPageSize.SelectedValue));

            switch (Status)
            {
                case (int)CommentType.IsPublished:
                    btnPublish.Visible = false;
                    btnRemove.Visible  = true;
                    break;
                case (int)CommentType.IsWaitingForPublish:
                    btnPublish.Visible = true;
                    btnRemove.Visible  = false;
                    break;
                default:
                    btnPublish.Visible = true;
                    btnRemove.Visible  = true;
                    break;
            }

            if (!IsPostBack)
            {                
                ListData();
                GetStatistics();
            }    
        }

        #region Statistics

        private void GetStatistics()
        {            
            Publish             = CommentController.Comment_GetStatistic((int)CommentType.IsPublished);
            WaittingForPublish  = CommentController.Comment_GetStatistic((int)CommentType.IsWaitingForPublish);            
        }

        #endregion

        #region List Data

        private void ListData()
        {
            var totalRecords = 0;
            lvList.DataSource = CommentController.Comment_GetListSeach(TypeId, Status, PageIndex, PageSize, ref totalRecords);
            lvList.DataBind();

            ltPager.Text = PagerUtility.PageLinks(PageIndex, PageSize, totalRecords, 5,
                                UrlHelpers.ListSearchComment(cControl.AdminMenuId, TypeId, Status, "{0}", PageSize));
            
            ddlPageSize.SelectedValue = PageSize.ToString();
            ddlStatus.SelectedValue   = Status.ToString();
            ddlType.SelectedValue     = TypeId.ToString();
        }

        #endregion

        private void UpdateStatus(int targetStatus)
        {
            ltMessage.Text = string.Empty;
            var count = 0;
            foreach (ListViewDataItem lvi in lvList.Items)
            {
                var cbItem = (CheckBox)lvi.FindControl("cbItem");
                if (cbItem == null || !cbItem.Checked) continue;

                count++;
                var id = ConvertUtility.ToInt32(lvList.DataKeys[lvi.DisplayIndex].Value);
                if (id > 0)
                {
                    var o = CommentController.Comment_GetById(id);
                    if (o != null)
                        CommentController.Comment_Update_Status(id, targetStatus);
                }
            }

            if (count == 0)
                ltMessage.Text = MessageUtility.ShowMessage(string.Empty, "Bạn chưa chọn bình luận nào.", MessageType.Warning);

            ListData();
            GetStatistics();
        }

        protected void BtnPublish(object sender, EventArgs e)
        {
            UpdateStatus((int)CommentType.IsPublished);
        }

        protected void BtnRemove(object sender, EventArgs e)
        {
            UpdateStatus((int)CommentType.IsWaitingForPublish);
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            RedirecLink();
        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            RedirecLink();
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            RedirecLink();
        }

        public void RedirecLink()
        {
            var status   = ConvertUtility.ToInt32(ddlStatus.SelectedValue);
            var typeId   = ConvertUtility.ToInt32(ddlType.SelectedValue);
            var pageSize = ConvertUtility.ToInt32(ddlPageSize.SelectedValue);            
            Response.Redirect(UrlHelpers.ListSearchComment(cControl.AdminMenuId, typeId, status, SettingValues.PageIndex.ToString(), pageSize));    
        }

        protected void lvList_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (string.Equals(e.CommandName, "DeleteComment"))
            {
                var dataItem = (ListViewDataItem)e.Item;
                var id = ConvertUtility.ToInt32(lvList.DataKeys[dataItem.DisplayIndex].Value);

                if (id > 0)
                {
                    CommentController.Comment_Delete(id);
                    ltMessage.Text = MessageUtility.ShowMessage(null, "Xóa bình luận thành công.", MessageType.Information);
                }
            }
            ListData();
        }

        protected void lvList_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var ltLinkComment = (Literal) e.Item.FindControl("ltLinkComment");
                var body          = ((HiddenField) e.Item.FindControl("hfBody")).Value;
                var typeId        = ConvertUtility.ToInt32(((HiddenField) e.Item.FindControl("hfTypeId")).Value);
                var id            = ConvertUtility.ToInt32(((HiddenField) e.Item.FindControl("hfId")).Value);
                
                if (id > 0)
                {
                    switch (typeId)
                    {
                        case 1: // Sản phẩm
                            var p = ProductController.Product_GetById(id);
                            if (p != null)
                                ltLinkComment.Text = string.Format("<a href=\"{0}{1}\" target=\"_blank\">{2}</a>",
                                                       WebConfigurationManager.AppSettings["Domain"], UrlHelpers.ProductDetail(CategoryController.GetCategoryName(p.CategoryId), id, p.Name), body);                                
                            break;
                        case 2: // Tin tức
                            var a = ArticleController.Article_GetById(id);
                            if (a != null)
                                ltLinkComment.Text = string.Format("<a href=\"{0}{1}\" target=\"_blank\">{2}</a>",
                                                        WebConfigurationManager.AppSettings["Domain"], UrlHelpers.ArticleDetail(CategoryController.GetCategoryName(a.CategoryId), id, a.Title), body);                                
                            break;
                    }
                }
            }
        }
    }
}